/**
 * 文本行类
 */
var TextLine = function(x, y) {
    this.text = '';
    this.left = x;
    this.bottom = y; //是bottom而不是top，因为文本填充原点默认在文本矩形的左下角
    this.caret = 0;
}
TextLine.prototype = {
    insert: function(text) {
        this.text = this.text.substr(0, this.caret) + text + this.text.substr(this.caret);
        this.caret += text.length;
    },
    removeCharacterBeforeCaret: function() {
        this.text = this.text.substring(0, this.caret - 1) + this.text.substring(this.caret);
        this.caret--;
    },
    getWidth: function(ctx) {
        return ctx.measureText(this.text).width;
    },
    getHeight: function(ctx) {
        var h = ctx.measureText('W').width;
        return h + h / 6;
    },
    draw: function(ctx) {
        var curTop = this.bottom + this.getHeight(ctx) / 2;
        ctx.save();
        ctx.textAlign = 'start';
        ctx.textBaseline = 'bottom';
        ctx.strokeText(this.text, this.left, curTop);
        ctx.fillText(this.text, this.left, curTop);
        ctx.restore();
    },
    erase: function(ctx, imageData) {
        ctx.putImageData(imageData, 0, 0);
    }
}